﻿/*
   Copyright 2012 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web;
using uk.ac.soton.ses.DAL;

namespace uk.ac.soton.ses.WebControls
{
    /// <summary>
    /// Lists the data items within an experiment formatted for a printing in a report. Do not use it on a page that causes postbacks.
    /// </summary>
    public class DataItemReportingControl : CompositeDataBoundControl
    {
        /// <summary>
        /// Generates the controls for the data items part of the report. This is not meant to be used with postbacks, so controls are only created when databinding.
        /// </summary>
        /// <param name="dataSource">An IEnumerable ExperimentDataFile</param>
        /// <param name="dataBinding">Whether we are dataBinding. This control only supports true.</param>
        /// <returns>The count of records for use on a postback (which are not supported)</returns>
        protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
        {
            int count = 0;
            IEnumerable<ExperimentDataFile> experimentFiles = null;
            MaterialsMetadataContext dbcontext = HttpContext.Current.GetMaterialsDBContext();
            if (dataSource != null)
            {
                /* --We have a dataSource. Find out how many records we need to display in our table--*/
                if (dataBinding)
                {
                    experimentFiles = dataSource.OfType<ExperimentDataFile>();
                    count = experimentFiles.Count();
                }
                else
                {
                    //If we're not dataBinding, but we have a dataSource, then this must be a postback.
                    //We therefore just have a dummy datasource with no real data.
                    //Because we need data to recreate the controls, we cannot proceed any further.
                    Label lbl = new Label();
                    Controls.Add(lbl);
                    lbl.Text = "DataItemReportingControl should not be used on web pages that cause post backs.";
                    count = 0;
                    return count;
                }

                if (count > 0)
                {
                    Table table = new Table();
                    Controls.Add(table);
                    TableRow tr1 = null;
                    TableRow tr2 = null;

                    int currentImage = 0;
                    foreach (ExperimentDataFile passedExperimentDataFile in experimentFiles)
                    {
                        currentImage++;

                        if ((currentImage-1) % 2 == 0) //Odd numbered images have a new row in the table
                        {
                            tr1 = new TableRow();
                            table.Rows.Add(tr1);
                            tr2 = new TableRow();
                            table.Rows.Add(tr2);
                        }

                        //Add controls:
                        TableCell td1 = new TableCell();
                        tr1.Cells.Add(td1);
                        TableCell td2 = new TableCell();
                        tr2.Cells.Add(td2);

                        Image imgDataItem = new Image();
                        imgDataItem.CssClass = "ReportImageInTable";
                        td1.Controls.Add(imgDataItem);
                        Label lblImageDescription = new Label();
                        lblImageDescription.CssClass = "ReportImageDescription";
                        td2.Controls.Add(lblImageDescription); //Always add the control before changing data so the ViewState can be tracked

                        //Data bind the controls:
                        imgDataItem.ImageUrl = HDCConfiguration.GetUrl_BasicImageHandler(passedExperimentDataFile.ID, "png");

                        //Add the file name
                        lblImageDescription.Text = string.Format("<b>{0}</b>", HttpUtility.HtmlEncode(passedExperimentDataFile.Filename));

                        //If there is a description, print it
                        if (!string.IsNullOrEmpty(passedExperimentDataFile.Description))
                        {
                            lblImageDescription.Text += string.Format("<br/>{0}", HttpUtility.HtmlEncode(passedExperimentDataFile.Description));
                        }
                    }
                }
            }
            return count;
        }
    }
}
